//
//  visitorView.swift
//  XinLang
//
//  Created by 张辉 on 16/5/11.
//  Copyright © 2016年 张辉. All rights reserved.
//

import UIKit
import SnapKit

///设置访客视图的协议
protocol visitorViewDelegate:NSObjectProtocol {
    
    //设置访客视图注册按钮
    func visitorViewRegisterButton()
    //设置访客视图登录按钮
    func visitorViewLoginButton()
    
}


class VisitorView: UIView {

    
  
    ///遵守代理
    weak  var delegate :visitorViewDelegate?
    //MARK:---控制器的生命周期
    override init(frame: CGRect) {

        super.init(frame:frame)
        backgroundColor = UIColor.whiteColor()
        //设置控件
        setUpUI()
        
//        ///按钮的监听
        registerButton.addTarget(self, action:#selector(VisitorView.clickRegisterButton), forControlEvents: .TouchUpInside)
        ///按钮的监听
        loginButton.addTarget(self, action:#selector(VisitorView.clickLoginButton), forControlEvents: .TouchUpInside)
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    // MARK: - 监听方法
    @objc private func clickRegisterButton (){
     
       delegate?.visitorViewRegisterButton()
        
    
    }
    
    @objc private func clickLoginButton(){

    delegate?.visitorViewLoginButton()
       
    
    }
    
    
    //MARK:---根据不同控制器返回不同的访客视图
    // MARK: - 设置视图信息
    /// 设置访客视图信息
    ///
    /// - parameter imageName: 图像名称，如果是首页传入 nil
    /// - parameter message:   消息文字
    func setUpInfo(imageName :String? = nil,message :String){
      //描述消息
        messageLabe.text = message
     
        guard let imageName = imageName  else {
            startAnimation()
            return
        }
        
        HomeImage.hidden = true
        maskIconImage.hidden = true
       //
        iconImage.image = UIImage(named: imageName)

    
    }
    /// 开始动画    
  private  func startAnimation(){
    
    let anm = CABasicAnimation(keyPath: "transform.rotation")
    
    anm.toValue = M_PI * 2
    
    anm.duration = 20
    
    anm.repeatCount = MAXFLOAT
    //运行测试，发现切换控制器后动画会被释放，另外在首页退出到桌面再次进入，动画同样会被释放
    //设置动画属性

    anm.removedOnCompletion = false
    iconImage.layer.addAnimation(anm, forKey: nil)
    
    }
    
    
    ///MARK:---私有控件
    //小风车图标
    private  lazy var iconImage :UIImageView = UIImageView(zh_imageName: "visitordiscover_feed_image_smallicon")
    //遮盖视图图片
    private  lazy var maskIconImage :UIImageView = UIImageView(zh_imageName: "visitordiscover_feed_mask_smallicon")
    
    // 小房子图标
    private lazy var HomeImage :UIImageView = UIImageView(zh_imageName: "visitordiscover_feed_image_house")
    
    //描述文字
    private lazy var messageLabe : UILabel = UILabel(
        zh_text: "关注一些人，回这里看看有什么惊喜关注一些人，回这里看看有什么惊喜",
        textAlignment: .Center,
        textColor: UIColor.orangeColor(),
        fontsize: 14)
    
    //注册按钮
    
    private lazy var registerButton :UIButton = UIButton(zh_title: "注册",
        titleColor: UIColor.orangeColor(),
        backImageName: "common_button_white_disable")
        

    //登录按钮
    private lazy var loginButton :UIButton = UIButton(zh_title: "登录",
            titleColor: UIColor.orangeColor(),
             backImageName: "common_button_white_disable")

    
}

extension VisitorView {
    
    
    private  func setUpUI(){
        
        
        addSubview(iconImage)
        addSubview(maskIconImage)
        addSubview(HomeImage)
        addSubview(messageLabe)
        addSubview(registerButton)
        addSubview(loginButton)
        
        //MARK:--- 设置约束
        // 图标 - 如果参照对象的属性一致，可以省略
        //风车
        iconImage.snp_makeConstraints { (make) in
            make.centerX.equalTo(self)
            make.centerY.equalTo(self).offset(-60)
            
        }
        //小房子
        HomeImage.snp_makeConstraints { (make) in
            make.center.equalTo(iconImage)
        }
        //信息lable
        messageLabe.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(iconImage)
            make.top.equalTo(iconImage.snp_bottom).offset(12)
            make.size.equalTo(CGSize(width: 224, height:36))
            
        }
        //注册按钮
        registerButton.snp_makeConstraints { (make) in
            make.left.equalTo(messageLabe.snp_left)
            make.top.equalTo(messageLabe.snp_bottom).offset(20)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        
        //登录按钮
        loginButton.snp_makeConstraints { (make) in
            make.right.equalTo(messageLabe.snp_right)
            make.top.equalTo(registerButton.snp_top)
            make.size.equalTo(CGSize(width: 100, height: 36))
        }
        //遮盖视图图片
        maskIconImage.snp_makeConstraints { (make) in
          
            make.left.equalTo(self)
            make.right.equalTo(self)
            make.top.equalTo(self)
            make.bottom.equalTo(registerButton.snp_bottom)
            
        }
     
        backgroundColor = UIColor(white: 237.0 / 255.0, alpha: 1.0)
    }
    
    
    
    
    
    
    
    
}